﻿using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.Project;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core;
using JESAI.Core.Attributes;
using JESAI.Platform.Domain.Admin.Entities;
using JESAI.Platform.Domain.Admin.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Admin
{
    [AppService]
    public class ProjectService : IProjectService
    {
        private readonly IProjectRepository _projectRepository;

        public ProjectService(IProjectRepository projectRepository)
        {
            _projectRepository = projectRepository;
        }

        public void DeleteProject(long id)
        {
            _projectRepository.DeleteById(id);
        }

        public void DeleteProjects(long[] ids)
        {
            _projectRepository.DeleteByIds(ids as dynamic);
        }

        public List<ProjectDto> QueryProject()
        {
            var modelList = _projectRepository._DbQueryable.ToList();
            return modelList.MapTo<List<ProjectDto>>();
        }

        public void UpdateOrInsert(SetProjectInput input, long userId)
        {
            var model = input.MapTo<ProjectModel>();
            if (model.Id > 0)
            {
                // 基础字段不容许更新
                model.LastTime = DateTime.Now;
                model.LastUid = userId;
                _projectRepository.AsUpdateable(model)
                               .IgnoreColumns(it => new { it.CreateUid, it.CreateTime })
                               .ExecuteCommand();
            }
            else
            {
                model.CreateTime = DateTime.Now;
                model.CreateUid = userId;
                model.LastTime = DateTime.Now;
                model.LastUid = model.CreateUid;
                _projectRepository.AsInsertable(model)
                               .ExecuteCommand();
            }
        }
    }
}
